//
// Created on 2024/9/7.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef OHOS_DEMO_PEER_CONNECTION_DEPDENCY_FACTORY_H
#define OHOS_DEMO_PEER_CONNECTION_DEPDENCY_FACTORY_H

#include "api/peer_connection_interface.h"
#include "rtc_base/thread.h"
#include "rtc_client/ohos_local_stream.h"
#include "sdk/ohos/api/ohos_peer_connection_factory_interface.h"
#include "sdk/ohos/api/ohos_peer_connection_interface.h"
#include "capturer/njs_camera_capture.h"
#include "rtc_client/rtc_wrapper.h"

namespace ohosrtc {

using OHOSLocalStreamPtr = rtc::scoped_refptr<OHOSLocalStream>;

using OHOSAudioTrackPtr = rtc::scoped_refptr<ohoswebrtc::OHOSAudioTrackInterface>;
using OHOSVideoSourcePtr = rtc::scoped_refptr<ohoswebrtc::OHOSVideoSourceInterface>;
using OHOSVideoTrackPtr = rtc::scoped_refptr<ohoswebrtc::OHOSVideoTrackInterface>;
using OHOSVideoRender = std::shared_ptr<webrtc::VideoRenderInterface>;
using ThreadPtr = std::unique_ptr<rtc::Thread>;
using AudioDeviceModulePtr = webrtc::scoped_refptr<webrtc::AudioDeviceModule>;
using OHOSPeerConnectionFactoryPtr = rtc::scoped_refptr<ohoswebrtc::OHOSPeerConnectionFactoryInterface>;
using OHOSPeerConnectionPtr = rtc::scoped_refptr<ohoswebrtc::OHOSPeerConnectionInterface>;

// webrtc PeerConnectionFactory wrapper
class PeerConnectionDependencyFactory {
  public:
    // create PeerConnectionFactory
    static std::shared_ptr<PeerConnectionDependencyFactory> Create();
    // create PeerConnectionFactory
    void CreateOHOSPeerConnectionFactory();
    // create PeerConnection
    OHOSPeerConnectionPtr CreateOHOSPeerConnection(RTCConfig config, ohoswebrtc::OHOSPeerConnectionObserver *observer);
    // create LocalStream for PeerConnection
    OHOSLocalStreamPtr CreateOHOSLocalStream(bool has_audio, bool video_capturer, bool is_screen_cast);
    // create VideoSource for PeerConnection
    OHOSVideoSourcePtr CreateOHOSVideoSource(bool is_screen_cast);
    // create LocalAudioTrack for PeerConnection
    OHOSAudioTrackPtr CreateOHOSLocalAudioTrack();
    // create LocalVideoTrack for PeerConnection use VideoSource
    OHOSVideoTrackPtr CreateOHOSLocalVideoTrack(OHOSVideoSourcePtr video_source);
    OHOSLocalStreamPtr GetLocalStream() {
      return ohos_local_stream_;
    }
  
    OHOSVideoSourcePtr GetVideoSource() {
      return video_source_;
    }
     
    OHOSVideoSourcePtr GetScreenSource() {
      return screen_source_;
    }
  
    OHOSPeerConnectionFactoryPtr GetOHOSPeerConnectionFactory() {
      return ohos_pc_factory_;
    }
  
  private:
    std::string GenerateRandomId(int length);
    OHOSLocalStreamPtr CreateOHOSLocalStream(bool is_screen_cast);   
    OHOSVideoSourcePtr video_source_;
    OHOSVideoSourcePtr screen_source_;  
    OHOSLocalStreamPtr ohos_local_stream_;  
    ThreadPtr signaling_thread_;
    OHOSPeerConnectionFactoryPtr ohos_pc_factory_;
    AudioDeviceModulePtr adm_;
    OHOSPeerConnectionPtr ohos_pc_;
  
};
} // namespace ohosrtc

#endif // OHOS_DEMO_PEER_CONNECTION_DEPDENCY_FACTORY_H
